﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using CrystalDecisions.CrystalReports.Engine;
namespace IceAge
{
    public partial class ReportScreen : DockContent
    {
        public ReportScreen()
        {
            InitializeComponent();
        }
        private string report_name = string.Empty;
        public string ReportName
        {
            set
            {
                report_name = value;
            }
        }

        public void reset_filter()
        {
            try
            {
                txtFromDate.Enabled = false;
                txtFromDate.ResetText();
                txtFromDate.Checked = true;

                txtToDate.Enabled = false;
                txtToDate.ResetText();
                txtToDate.Checked = true;

                txtClientId.Enabled = false;
                txtClientId.ResetText();
                txtBankAccount.Enabled = false;
                txtBankAccount.ResetText();
                txtFirmCode.Enabled = false;
                txtFirmCode.Text = CommonLibrary.FIRM_CODE + "C";

                cboContractType.SelectedIndex = cboContractType.Items.Count - 1;
                cboAccountType.SelectedIndex = cboAccountType.Items.Count - 1;
                cboBoard.SelectedIndex = cboBoard.Items.Count - 1;

                DataSet report_control = CommonLibrary.GetDataSetFromProcedure("sp_sel_ReportControls",
                "'" + this.Tag.ToString() + "'");
                foreach (DataRow row in report_control.Tables[0].Rows)
                {
                    switch (Convert.ToInt32(row["FilterControl"]))
                    {
                        case 1:
                            txtFromDate.Enabled = true;
                            break;
                        case 2:
                            txtToDate.Enabled = true;
                            break;
                        case 3:
                            txtFirmCode.Enabled = true;
                            txtClientId.Enabled = true;
                            break;
                        case 4:
                            txtBankAccount.Enabled = true;
                            break;
                        case 5:
                            cboBoard.Enabled = true;
                            break;
                        case 6:
                            cboSecurity.Enabled = true;
                            break;
                        case 7:
                            cboAccountType.Enabled = true;
                            break;
                        case 8:
                            cboContractType.Enabled = true;
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); 
            }
            
        }
        private void report_preview()
        {
            //CenturyFox.Service data_access = new CenturyFox.Service();
            string account_no = string.Empty;
            if (txtClientId.Text.Trim() != string.Empty)
                account_no = txtFirmCode.Text + txtClientId.Text;
            DataSet report_source = CommonLibrary.GetDataSetFromProcedure("sp_sel_ReportSource",
                "'" + this.Tag.ToString() + "','" + txtFromDate.Value.ToString("dd-MMM-yyyy") + "','" +
                txtToDate.Value.ToString("dd-MMM-yyyy") +"','"+
                    //((DataRowView)cboBoard.SelectedItem)["ITEM_VALUE"].ToString(), 
                    string.Empty +"','"+
                    cboSecurity.Text +"','"+
                    //((DataRowView)cboAccountType.SelectedItem)["ITEM_VALUE"].ToString(), 
                    string.Empty +"','"+
                    //((DataRowView)cboContractType.SelectedItem)["ClntTypeCode"].ToString(),
                    string.Empty +"','"+
                    account_no +"','"+ txtBankAccount.Text +"'");
            CrystalDecisions.CrystalReports.Engine.ReportClass v_report =
                (CrystalDecisions.CrystalReports.Engine.ReportClass)Activator.CreateInstance(
                Type.GetType(this.report_name, true, true));

            v_report.SetDataSource(report_source.Tables[1]);
            if (report_source.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in report_source.Tables[0].Rows)
                {
                    v_report.SetParameterValue(row["ParameterName"].ToString(),
                        report_source.Tables[2].Rows[0][row["FieldName"].ToString()]);
                }
            }

            v_report.SetParameterValue("@company", CommonLibrary.COMPANY_NAME);
            v_report.SetParameterValue("@address", CommonLibrary.ADDRESS);
            v_report.SetParameterValue("@website", CommonLibrary.CONTACT);
            ReportPreview obj = new ReportPreview();
            obj.Text = this.Text;
            obj.crystalReportViewer1.ReportSource = v_report;
            obj.Show(this.DockPanel, DockState.Document);
            //foreach (Form childForm in Application.OpenForms)
            //{
            //    if (childForm.Name == "MDIForm")
            //    {
            //        obj.Show(((MDIForm)childForm).dockPanel1, DockState.Document);
            //        return;
            //    }
            //}
            
        }
        private void ReportScreen_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.F9)
                {
                    report_preview();
                }                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); 
            }
            
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtBankAccount.ResetText();
            txtClientId.ResetText();
            cboSecurity.ResetText();
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            try
            {
                report_preview();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ReportScreen_Load(object sender, EventArgs e)
        {
            try
            {
                DataSet client_type = CommonLibrary.GetDataSetFromProcedure("sp_sel_ReportInitial",string.Empty);
                DataRow row = client_type.Tables[0].NewRow();
                row["ClntTypeDesc"] = string.Empty;
                row["ClntTypeCode"] = string.Empty;
                client_type.Tables[0].Rows.Add(row);
                cboContractType.DataSource = client_type.Tables[0];
                cboContractType.DisplayMember = "ClntTypeDesc";
                cboContractType.ValueMember = "ClntTypeCode";

                row = client_type.Tables[1].NewRow();
                row["Item_value"] = string.Empty;                
                client_type.Tables[1].Rows.Add(row);
                cboBoard.DataSource = client_type.Tables[1];
                cboBoard.DisplayMember = "Item_value";
                cboBoard.ValueMember = "Item_value";

                row = client_type.Tables[2].NewRow();
                row["Item_value"] = string.Empty;
                row["DESCRIPTION"] = string.Empty;
                client_type.Tables[2].Rows.Add(row);
                cboAccountType.DataSource = client_type.Tables[2];
                cboAccountType.DisplayMember = "DESCRIPTION";
                cboAccountType.ValueMember = "Item_value";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}